package cn.thr.ymcc.consumer;

import cn.thr.ymcc.constant.Cos;
import cn.thr.ymcc.dto.KillOrderTimeoutDto;
import cn.thr.ymcc.service.IKillCourseService;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "KillOrderTimeoutConsumer",
        topic = Cos.Kill.TOPIC_KILL_ORDER_TIMEOUT,
        selectorExpression = Cos.Kill.TAGS_KILL_ORDER_TIMEOUT
)
public class KillOrderTimeoutConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IKillCourseService killCourseService;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String jsonStr = new String(body,"utf-8");

            log.info("秒杀服务消费者，正在处理秒杀下单超时业务..{}",jsonStr);

            KillOrderTimeoutDto killOrderTimeoutDto = JSON.parseObject(jsonStr,KillOrderTimeoutDto.class);

            killCourseService.handleKillOrderTimeout(killOrderTimeoutDto);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
